package com.lagou.controller;

import com.lagou.pojo.Resume;
import com.lagou.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

    @Autowired
    private ResumeService resumeService;


    @RequestMapping("/login.do")
    public ModelAndView login(String username, String password, HttpServletRequest request){
        String admin="admin";

        if (admin.equals(username)&&admin.equals(password)){
           request.getSession().setAttribute("resume","admin");
        }else {
            return new ModelAndView(new RedirectView("/index.jsp"));

        }

        return new ModelAndView(new RedirectView("/listAll.do"));

    }



    @RequestMapping("/listAll.do")
    public String listAll(Map<String,Object> map){
         List<Resume> all = resumeService.findAll();
          map.put("list",all);
        return "userList.jsp";
    }



    @RequestMapping(value = "add",method = RequestMethod.POST)
    public String add(Resume resume,Map<String,Object> map){
        resumeService.add(resume);
        map.put("resume",resume);
        return "redirect:/listAll.do";
    }


    @RequestMapping(value = "addIndex",method = RequestMethod.GET)
    public String addIndex(Map<String,Object> map){

        map.put("resume",new Resume());
        return "/input.jsp";
    }

    @RequestMapping("/update.do")
    public Integer update(Resume resume){

        return resumeService.update(resume);
    }



    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id")Long id){
        resumeService.delete(id);

        return  "redirect:/listAll.do";
    }


    @RequestMapping("/findById/{id}")
    public String findbyId(@PathVariable("id") Long id, Map<String,Object> map){

         Resume resume = resumeService.findById(id);
         map.put("resume",resume);

        return "/input.jsp";
    }


}
